package com.github.sevenheaven.gesturelock.widget.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.MenuItem;
import com.github.sevenheaven.gesturelock.widget.GestureLockUtils;

/**
 * Created by ShinChven on 14/9/1.
 */
public class AutoLockActionBarActivity extends ActionBarActivity {


    private static final String TAG = "AutoLockActionBarActivity";

    public static final String IS_LOCK = "is_unlock";
    public static final int LOCK_SCREEN = 992;
    private boolean lock = true;

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume " + getLocalClassName() + " " + lock);
        // todo enable or disable auto locker here
        if (GestureLockUtils.isEnabled(AutoLockActionBarActivity.this)) {
            if (GestureLockUtils.isLocked() && GestureLockUtils.getGesture(this) != null) {
                Intent intent = new Intent(this, LockActivity.class);
                startActivityForResult(intent, LOCK_SCREEN);
            }
        }
//        else if(GestureLockUtils.getGesture(this) == null){
//            Intent intent = new Intent(this, SetupPatternActivity.class);
//            startActivity(intent);
//        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate " + getLocalClassName() + " " + lock);

        unlockActivity();
    }

    private void unlockActivity() {
        try {
            Bundle extras = getIntent().getExtras();
            boolean tempLock = extras.getBoolean(IS_LOCK, true);
            GestureLockUtils.setLocked(tempLock);
        } catch (Exception e) {
            //ApplicationExtend.setLocked(true);
        }
    }

    @Override
    public void startActivity(Intent intent) {
        intent.putExtra(IS_LOCK, false);
        Log.i(TAG, "startActivity " + getLocalClassName() + " " + lock);

        super.startActivity(intent);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        intent.putExtra(IS_LOCK, false);
        Log.i(TAG, "startActivityForResult " + getLocalClassName() + " " + lock);

        super.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "onStop " + getLocalClassName() + " " + lock);
        GestureLockUtils.setLocked(lock);
    }

    @Override
    public void finish() {
        super.finish();
        GestureLockUtils.setLocked(false);
        Log.i(TAG, "finish " + getLocalClassName() + " " + lock);

    }

    @Override
    public void onBackPressed() {
        GestureLockUtils.setLocked(false);
        super.onBackPressed();
        Log.i(TAG, "onBackPressed " + getLocalClassName() + " " + lock);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            lock = false;
            GestureLockUtils.setLocked(false);
            Log.i(TAG, "home " + getLocalClassName() + " " + lock);
        }
        return super.onOptionsItemSelected(item);
    }
}
